package leetcode.biweekly.week94;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;

//Solution2Test
public class Solution2 {

	public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report,
			int[] student_id, int k) {
		Set<String> pos = new HashSet<>();
		for (String s : positive_feedback) {
			pos.add(s);
		}
		Set<String> neg = new HashSet<>();
		for (String s : negative_feedback) {
			neg.add(s);
		}
		PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
		for (int i = 0; i < report.length; i++) {
			String[] rs = report[i].split(" ");
			int id = student_id[i];
			int score = 0;
			for (String s : rs) {
				if (pos.contains(s)) {
					score += 3;
				} else if (neg.contains(s)) {
					score -= 1;
				}
			}
			if (queue.size() < k) {
				queue.add(new int[] { score, id });
			} else {
				if (queue.peek()[0] < score || (queue.peek()[0] == score && queue.peek()[1] > id)) {
					queue.poll();
					queue.add(new int[] { score, id });
				}
			}
		}
		List<Integer> ans = new ArrayList<>();
		while (!queue.isEmpty()) {
			ans.add(0, queue.poll()[1]);
		}
		return ans;
	}

}
